/*
	2 - Searching page_results that can match the word filters
*/
function searchpage_results(){
	try{
		debug("Searching pages");
	
		for(var i=0;i<sources.length;i++){
			
			debug("Checking source "+sources[i].name);
					
			getHTML(sources[i].url);
		
			page_results=new Array();
		}
		
		pageResults();
	
	}catch(e){
		error("filterpage_results"+e.description);
	}
}

function analize_results(p_page){

	var result=analizeContent(p_page);
	
	if(result){
		var page_result=new Object();
	
		page_result.url=p_page.url;
		page_result.sumary=result;
			
		page_results[page_results.length]=page_result;
		
	}
	
	pageResults();
}


/*
	4 - Renderize the page_results results
*/
function pageResults(){

	try{		
		var page_resultsresult = $("#pagesresult");
		
		page_resultsresult.html("");
	
		debug("Loading page results");
		
		var html="";
		
		for(var i=0;i<page_results.length;i++){
			html+="<div>";
			
			html+="<p class='result'>";
			html+=page_results[i].sumary;
			html+="</p>";
			
			html+="<div class='links'>";
			html+=page_results[i].url + "<br>";
			
			for(var j=0;j<3&&j<services.length;j++){
				var service=services[j];
				html+="<b class='service' onclick=addFilterService('"+service.name+"')> "+unescape(service.name)+" </b> |";
			}
			html+="</div>";
			
			html+="</div>";
			html+="<br>";
		}
						
		page_resultsresult.html(html);
		
	}catch(e){
		error(e.description);
	}
}

/*
	7 - Searching for the available services
*/
function searchServices(){

	try{
		
		debug("Searching services");
		
		filterServices();
	
		debug("Loading services results");
		
		var serviceresult = $("#serviceresults");
		serviceresult.html("No results found for this service(s)...");
		
		var html="";
		
		for(var i=0;i<10&&i<services_results.length;i++){
			html+="<div>";
			html+="<p class='result'>";
			html+=services_results[i].sumary;
			html+="</p>";
			html+="</div>";
			html+="<br>";
		}
		
		serviceresult.html(html);
		
	}catch(e){
		error(e.description);
	}
}


//THIS IS A FUCKING MOCK
/*
	8 - Filtering for the found services that fits on the filter
*/
function filterServices(){
	try{
		debug("Filtering services");
		
		var service = new Object();
		service.url = "www.globo.com";
		service.sumary = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.";
		service.icon = "globo.com";

		for(var i=0;i<9;i++){
			services_results[i]=service;
		}
		
		debug(services_results.length+" services found");
		
	}catch(e){
		error(e.description);
	}
}